package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.ZhuanjiaRenkouhechaPiciMapper;
import com.ruoyi.system.mapper.ZhuanjiabiandongshenbaoMapper;
import com.ruoyi.system.mapper.ZhuanjiabiandongshenbaoQushiMapper;
import com.ruoyi.system.service.IShiwuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ZhuanjiaRenkouhechaMapper;
import com.ruoyi.system.service.IZhuanjiaRenkouhechaService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-31
 */
@Service
public class ZhuanjiaRenkouhechaServiceImpl implements IZhuanjiaRenkouhechaService 
{
    @Autowired
    private ZhuanjiaRenkouhechaMapper zhuanjiaRenkouhechaMapper;

    @Autowired
    private ZhuanjiabiandongshenbaoMapper zhuanjiabiandongshenbaoMapper;

    @Autowired
    private ZhuanjiabiandongshenbaoQushiMapper zhuanjiabiandongshenbaoQushiMapper;

    @Autowired
    private ZhuanjiaRenkouhechaPiciMapper zhuanjiaRenkouhechaPiciMapper;

    @Autowired
    private IShiwuService shiwuService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ZhuanjiaRenkouhecha selectZhuanjiaRenkouhechaById(String id)
    {
        return zhuanjiaRenkouhechaMapper.selectZhuanjiaRenkouhechaById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param zhuanjiaRenkouhecha 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ZhuanjiaRenkouhecha> selectZhuanjiaRenkouhechaList(ZhuanjiaRenkouhecha zhuanjiaRenkouhecha)
    {
        return zhuanjiaRenkouhechaMapper.selectZhuanjiaRenkouhechaList(zhuanjiaRenkouhecha);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param zhuanjiaRenkouhecha 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertZhuanjiaRenkouhecha(ZhuanjiaRenkouhecha zhuanjiaRenkouhecha)
    {
        return zhuanjiaRenkouhechaMapper.insertZhuanjiaRenkouhecha(zhuanjiaRenkouhecha);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param zhuanjiaRenkouhecha 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateZhuanjiaRenkouhecha(ZhuanjiaRenkouhecha zhuanjiaRenkouhecha)
    {
        return zhuanjiaRenkouhechaMapper.updateZhuanjiaRenkouhecha(zhuanjiaRenkouhecha);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteZhuanjiaRenkouhechaByIds(String ids)
    {
        return zhuanjiaRenkouhechaMapper.deleteZhuanjiaRenkouhechaByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteZhuanjiaRenkouhechaById(String id)
    {
        return zhuanjiaRenkouhechaMapper.deleteZhuanjiaRenkouhechaById(id);
    }

    public List<Map<String, Object>> qushiListBypiciid(String piciid, String danweimingcheng,
                                                       String danweicengji, String zhuanjiaxingming,
                                                       String gongzuodanwei, String shifouchuli,
                                                       String chuliriqi,
                                                       int pageindex, int pagesize) {
        int mmStartIndex = (pageindex - 1) * pagesize;
        return zhuanjiaRenkouhechaMapper.qushiListBypiciid(piciid,danweimingcheng, danweicengji,
                zhuanjiaxingming, gongzuodanwei,
                shifouchuli, chuliriqi,mmStartIndex, pagesize);
    }

    /**
     * @param piciid
     * @param danweimingcheng
     * @param danweicengji
     * @param zhuanjiaxingming
     * @param gongzuodanwei
     * @param shifouchuli
     * @param chuliriqi
     * @return
     */
    @Override
    public int countqushiListBypiciid(String piciid,String danweimingcheng,
                                      String danweicengji, String zhuanjiaxingming,
                                      String gongzuodanwei,
                                      String shifouchuli, String chuliriqi){
        return  zhuanjiaRenkouhechaMapper.countqushiListBypiciid(piciid,danweimingcheng, danweicengji,
                zhuanjiaxingming, gongzuodanwei,
                shifouchuli, chuliriqi);
    }

    @Override
    @Transactional
    public boolean addCustom(ZhuanjiaRenkouhecha ppRenkouhecha, Zhuanjiabiandongshenbao ppBiandong, ZhuanjiabiandongshenbaoQushi ppQushi, ZhuanjiaRenkouhechaPici ppPici){
        int addRenkouhecha = this.insertZhuanjiaRenkouhecha(ppRenkouhecha);
        if (addRenkouhecha!=1){
            throw new RuntimeException("去世确认添加失败");
        }

        int addBiandong = zhuanjiabiandongshenbaoMapper.insertZhuanjiabiandongshenbao(ppBiandong);
        if (addBiandong!=1){
            throw new RuntimeException("变动添加失败");
        }

        int addBiandongQushi = zhuanjiabiandongshenbaoQushiMapper.insertZhuanjiabiandongshenbaoQushi(ppQushi);
        if (addBiandongQushi!=1){
            throw new RuntimeException("变动去世添加失败");
        }

        int modifyPici = zhuanjiaRenkouhechaPiciMapper.updateZhuanjiaRenkouhechaPici(ppPici);
        if (modifyPici!=1){
            throw new RuntimeException("批次修改失败");
        }

        //添加事务
        Shiwu mmShiwu = new Shiwu();
        mmShiwu.setShiwuid(UUID.randomUUID().toString());
        mmShiwu.setDeptid(ppBiandong.getDeptid());
        mmShiwu.setIsqidong(0l);
        mmShiwu.setIsjieshu(0l);
        mmShiwu.setChuangjianshijian(new Date());
        mmShiwu.setBeizhu1(ppBiandong.getShenbaoid());
        mmShiwu.setIsvalid(1l);
        if (shiwuService.insertShiwu(mmShiwu)!=1){
            throw new RuntimeException("添加事务失败");
        }

        return true;
    }

    @Override
    public List<Map<String, Object>> qushiList(String jigoudaima, String danweimingcheng,
                                               String danweicengji, String zhuanjiaxingming,
                                               String gongzuodanwei,
                                               int shifouchuli, String chuliriqi,
                                               int pageindex, int pagesize) {
        int mmStartIndex = (pageindex - 1) * pagesize;
        return zhuanjiaRenkouhechaMapper.qushiList(jigoudaima,danweimingcheng, danweicengji,
                zhuanjiaxingming, gongzuodanwei,
                shifouchuli, chuliriqi, mmStartIndex, pagesize);
    }

    /**
     * @param jigoudaima
     * @param danweimingcheng
     * @param danweicengji
     * @param zhuanjiaxingming
     * @param gongzuodanwei
     * @param shifouchuli
     * @param chuliriqi
     * @return
     */
    @Override
    public int countqushiList(String jigoudaima, String danweimingcheng,
                              String danweicengji, String zhuanjiaxingming,
                              String gongzuodanwei,
                              int shifouchuli, String chuliriqi){
        return  zhuanjiaRenkouhechaMapper.countqushiList(jigoudaima,danweimingcheng, danweicengji,
                zhuanjiaxingming, gongzuodanwei,
                shifouchuli, chuliriqi);
    }
}
